# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

import pymysql
class DoubanPipeline:
    def __init__(self):
        self.connection = pymysql.connect(
            host='127.0.0.1',
            port=3306,
            user='root',
            password='123456',
            db='runboot',
            charset='utf8'
        )
        self.cursor = self.connection.cursor()
        self.cursor.execute('create table if not exists douban(title varchar(100),rating varchar(100),quote varchar(100))')
        self.connection.commit()

    def process_item(self, item, spider):
        self.cursor.execute('insert into douban(title,rating,quote) values(%s,%s,%s)',
                            (item['title'], item['rating'], item['quote']))
        self.connection.commit()
        return item

    def close_spider(self,spider):
        self.cursor.close()
        self.connection.close()
